<?php
include_once 'DataForm/DataForm.php';
include_once 'DataGrid/Form/FormSearch.php';
include_once 'DataGrid/DataGrid.php';

class Admin_Model_Stock extends NadebZend_Model_Crud
{
	public function __construct()
	{
    	$this->tb = new Application_Model_Db_Stock();
    	
    	parent::__construct();
	}
	
	public function data_grid()
	{
		$modelPath = __LINKS__ . 'admin/' . $this->_params->controller;
		$data = $this->get_all('idDetail','idDetail = '.$this->_params->relsub);
		
		$grid = new DataGrid( 'nadeb-grid' );
		$grid->title( 'Listagem de Estoque' );
		$grid->primary( $this->primary );
		$grid->setData( $data );
		$grid->setAction( $modelPath.'/delete/' );
		$grid->setControllers( new DeleteButton('delete_button', 'Excluir') );
		$grid->setControllers( new Link('insert_button', 'Inserir', $modelPath . '/edit/sub/' . $this->_params->sub . '/relsub/' . $this->_params->relsub) );
		$grid->setTools( new EditTool('Editar', $modelPath . '/edit/sub/' . $this->_params->sub . '/relsub/' . $this->_params->relsub) );
		$grid->setTools( new DeleteTool('Excluir', $modelPath . '/delete') );
		$grid->setHeader( array( 'Tamanho' => 'tm50', 'Estoque' ) );
		$grid->setRows( array(
							'idSize' => new GArray( $this->sizeOptions() ), 
							'quantity' ));
		return $grid->get();
	}
	
	public function data_form($id = null, $post = null)
	{
        $header      = HeaderController::get_instance();
		$header->js  = "public/admin/js/stock.js";
		$header->js  = "public/admin/js/jquery.price_format.1.5.js";
        
		$data = $id ? $this->get_one($id) : null;
		
		$form = new DataForm( 'nadeb-form' );
		$form->setData( $data );
		$form->title( 'Cadastro de Estoque' );
		$form->form()->action = __LINKS__ . 'admin/'. $this->_params->controller .'/edit/' . ( ($id) ? 'id/' . $id : '' );
		$form->add( new InputHidden('idDetail', $this->_params->relsub) );
		$form->add( new Select('idSize','Tamanho',$this->sizeOptions()) );
		$form->add( new InputText('sizeDescription','Descrição do tamanho (cm de frente)') );
		$form->add( new InputText('quantity','Estoque disponivél') );
		$form->fieldset('buttons');
		$form->add( new InputSubmit('btn_save', 'salvar') );
		$form->add( new InputButton('btn_cancel', 'cancelar') );
		
		if( $post )
		{
			$result = $this->save($id, $post, $form);
			return $result['message'];
		}
		else
		{
			return $form->get();
		}
	}
	
	function sizeOptions()
	{
		$tb = new Application_Model_Db_Sizes();
		$data = $tb->fetchAll('active = 1', 'order ASC');
		foreach ($data as $value) $result[$value->idSize] = $value->name;
		
		return $result;
	}
	
	public function populateStock($id)
	{
		if( !isset( $this->tb->fetchRow('idDetail = ' . $id)->idStock ) )
		{
			$tbSizes = new Application_Model_Db_Sizes();
			$dataSize = $tbSizes->fetchAll('active = 1', 'order ASC');
			foreach ( $dataSize as $size )
			{
				$row = $this->tb->fetchNew();
				$row->idDetail = $id;
				$row->idSize = $size->idSize;
				$row->quantity = 0;
				$row->save();
			}
			
		}
		return ;
	}
	
	public function breadCrumbs($action)
	{
		$bc = array(
			'list' => array(
				'' => 'home',
				'products/list' => 'Listagem de Produtos',
				'details/list/rel/' . $this->_params->sub => 'Detalhes do Produto',
				'#' => 'Estoque',
			),
				'edit' => array(
					'' => 'home',
					'products/list' => 'Listagem de Produtos',
					'details/list/rel/' . $this->_params->sub => 'Detalhes do Produto',
					'stock/list/relsub/' . $this->_params->relsub . '/sub/' . $this->_params->sub => 'Estoque',
					'#' => 'Cadastro de Estoque'
			)
		);
	
		return $bc[$action];
	}
}


